package com.feng.study.demo.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.feng.study.model.Cart;
import com.feng.study.model.Product;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * jackson使用demo
 * 1.@JsonFilter("product_filter")
 *      1.动态序列化指定字段。
 *      2.序列化的时候必须加上过滤器,否则会抛异常,json = objectMapper.writeValueAsString(cart)
 * 2.@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
 *      时间格式化注解
 * 3.@JsonProperty(value = "id")
 *      注解用于属性上，作用是把该属性的名称序列化为另外一个名称
 *      如把productId属性序列化为id，@JsonProperty(value="id")。
 * 4.@JsonInclude(JsonInclude.Include.NON_NULL)
 *      1.将该标记放在属性上，如果该属性为NULL则不参与序列化
 *      2.如果放在类上边,那对这个类的全部属性起作用
 * 5.@JsonIgnoreProperties(ignoreUnknown = true)
 *      忽略类中不存在的字段
 *      比如json字符串中有个aaa:123,则不会进行转换
 */
@Service
public class JacksonDemo {
    public static void main(String[] args) {
        demo01();
        demo03();
        demo04();
        demo05();
    }

    public static void demo01(){
        Product product1 = new Product().setName("苹果").setPrice("11").setProductId("001").setCreatTime(new Date());
        Product product2 = new Product().setName("西瓜").setPrice("5").setProductId("002").setCreatTime(new Date());
        Cart cart = new Cart();
        cart.setCartId("001").setAllNumber(4).setTotalPrice("99.99").setProductList(Lists.newArrayList(product1,product2));

        ObjectMapper objectMapper = new ObjectMapper();
        //过滤器
        SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider();
        simpleFilterProvider.addFilter("cart_filter", SimpleBeanPropertyFilter.serializeAllExcept("allNumber"));
//        simpleFilterProvider.addFilter("product_filter", SimpleBeanPropertyFilter.serializeAllExcept("price","creatTime"));
        objectMapper.setFilterProvider(simpleFilterProvider);

        String json ="";
        try {
            json = objectMapper.writeValueAsString(cart);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(json);
    }

    public static void demo03() {
        String json="{\n" +
                "  \"productId\":\"111\",\n" +
                "  \"name\":\"222\"\n" +
                "}";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Product product = objectMapper.readValue(json, Product.class);
            System.out.println(product);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
    public static void demo04() {
        Product product1 = new Product().setName("苹果").setProductId("001").setCreatTime(new Date());
        Product product2 = new Product().setName("西瓜").setPrice("5").setProductId("002").setCreatTime(new Date());
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String s = objectMapper.writeValueAsString(product1);
            String s2 = objectMapper.writeValueAsString(product2);
            System.out.println(s);
            System.out.println(s2);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
    public static void demo05() {
        String json="{\n" +
                "  \"productId\":\"111\",\n" +
                "  \"name\":\"222\",\n" +
                "  \"aaa\":\"333\"\n" +
                "}";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Product product = objectMapper.readValue(json, Product.class);
            System.out.println(product);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
